package Dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import utils.DBUtils;

public class LocationDao {
	private DBUtils dbUtils;
	public LocationDao(){
		dbUtils=DBUtils.getInstance();
	}
	public int insertLocation(int id,String location){
		int statusCode=-1;
		dbUtils.getConnection();
		String sql="select * from user_location_tb where user_id= ? ";
		List<Object> params=new ArrayList<Object>();
		params.add(id);
		try {
			Map<String,Object> tempResult=new HashMap<String,Object>();
			tempResult=dbUtils.queryOneMap(sql, params);
			params.clear();
			params.add(location);
			params.add(id);
			if(!tempResult.isEmpty()){
				sql=" update user_location_tb  set user_location=? where user_id=? ";
			}
			else{
			sql=" insert into user_location_tb (user_location,user_id) values(?,?) ";
			}
			if(dbUtils.updateOrDelSQL(sql, params)){
				statusCode=1;
			}else{
			statusCode=0;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			return -2;
		}
		return statusCode;
	}
}
